package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.WxAddressService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Project:Project2
 * @Title:WxAddressController
 * @Package:com.cskaoyan.market.controller
 * @Author:Cl
 * @Date :2024/5/20 21:34
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressService wxAddressService;
    @Autowired
    SecurityManager securityManager;

    @GetMapping("list")
    public Object list() {
        return ResponseUtil.okList(wxAddressService.list());
    }

    @GetMapping("detail")
    public Object detail(Integer id) {
        return ResponseUtil.ok(wxAddressService.detail(id));
    }

    @PostMapping("save")
    public Object save(@RequestBody MarketAddress marketAddress) {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        marketAddress.setUserId(user.getId());

        if (!(marketAddress.getId() == 0)) {
            //  执行更改
            return ResponseUtil.ok(wxAddressService.update(marketAddress));
        } else {
            //  执行创建
            return ResponseUtil.ok(wxAddressService.create(marketAddress));
        }
    }

    @PostMapping("delete")
    public Object delete(@RequestBody MarketAddress marketAddress) {
        wxAddressService.delete(marketAddress.getId());
        return ResponseUtil.ok();
    }

}
